{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Untitled79.ipynb",
      "provenance": [],
      "toc_visible": true,
      "authorship_tag": "ABX9TyPpZwmfhXnhPFhKVZ2jBcGU",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "TPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/ykitaguchi77/Colab_Scripts/blob/master/Convert%20Pytorch%20models%20to%20TFLite.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "o2r6hxOBivfj"
      },
      "source": [
        "#**Convert Pytorch models to TFLite**<br>\n",
        "https://qiita.com/lain21/items/9f9f9707ebad4bbc627d\n",
        "https://qiita.com/hirune924/items/06520f44927b0844a86c"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vxziBIrusC_Y",
        "outputId": "637af41d-28fd-4bde-98ea-3781948398f2"
      },
      "source": [
        "#google driveをcolabolatoryにマウント\n",
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": 51,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VAbUA0nq5hNP"
      },
      "source": [
        "#Pytorch to ONNX"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "khJAnT3q5sW4"
      },
      "source": [
        "https://colab.research.google.com/gist/amahendrakar/6042511a73aa403109ff22452502ffc5/46285.ipynb#scrollTo=rDAI_Iqu7Ucx <br>\n",
        "https://ichi.pro/pytorch-o-tensorflowlite-ni-henkansuru-watashi-no-tabi-14449401839341<br>\n",
        "https://qiita.com/hirune924/items/06520f44927b0844a86c <br>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "P09aBXw6EVIS"
      },
      "source": [
        "model_name = \"resnet50\""
      ],
      "execution_count": 52,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3z1yCtoYjC93",
        "outputId": "f4fbe1cd-32fa-46a9-f801-650cd416a455"
      },
      "source": [
        "import torch\n",
        "import torchvision.models as models\n",
        "\n",
        "dummy_input = torch.randn((1, 3, 224, 224))\n",
        "model = models.resnet50()\n",
        "torch.onnx.export(model, dummy_input, \"converted.onnx\", verbose=True, input_names=['input'], output_names=['output'])"
      ],
      "execution_count": 53,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "graph(%input : Float(1, 3, 224, 224, strides=[150528, 50176, 224, 1], requires_grad=0, device=cpu),\n",
            "      %fc.weight : Float(1000, 2048, strides=[2048, 1], requires_grad=1, device=cpu),\n",
            "      %fc.bias : Float(1000, strides=[1], requires_grad=1, device=cpu),\n",
            "      %497 : Float(64, 3, 7, 7, strides=[147, 49, 7, 1], requires_grad=0, device=cpu),\n",
            "      %498 : Float(64, strides=[1], requires_grad=0, device=cpu),\n",
            "      %500 : Float(64, 64, 1, 1, strides=[64, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %501 : Float(64, strides=[1], requires_grad=0, device=cpu),\n",
            "      %503 : Float(64, 64, 3, 3, strides=[576, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %504 : Float(64, strides=[1], requires_grad=0, device=cpu),\n",
            "      %506 : Float(256, 64, 1, 1, strides=[64, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %507 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %509 : Float(256, 64, 1, 1, strides=[64, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %510 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %512 : Float(64, 256, 1, 1, strides=[256, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %513 : Float(64, strides=[1], requires_grad=0, device=cpu),\n",
            "      %515 : Float(64, 64, 3, 3, strides=[576, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %516 : Float(64, strides=[1], requires_grad=0, device=cpu),\n",
            "      %518 : Float(256, 64, 1, 1, strides=[64, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %519 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %521 : Float(64, 256, 1, 1, strides=[256, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %522 : Float(64, strides=[1], requires_grad=0, device=cpu),\n",
            "      %524 : Float(64, 64, 3, 3, strides=[576, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %525 : Float(64, strides=[1], requires_grad=0, device=cpu),\n",
            "      %527 : Float(256, 64, 1, 1, strides=[64, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %528 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %530 : Float(128, 256, 1, 1, strides=[256, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %531 : Float(128, strides=[1], requires_grad=0, device=cpu),\n",
            "      %533 : Float(128, 128, 3, 3, strides=[1152, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %534 : Float(128, strides=[1], requires_grad=0, device=cpu),\n",
            "      %536 : Float(512, 128, 1, 1, strides=[128, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %537 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %539 : Float(512, 256, 1, 1, strides=[256, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %540 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %542 : Float(128, 512, 1, 1, strides=[512, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %543 : Float(128, strides=[1], requires_grad=0, device=cpu),\n",
            "      %545 : Float(128, 128, 3, 3, strides=[1152, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %546 : Float(128, strides=[1], requires_grad=0, device=cpu),\n",
            "      %548 : Float(512, 128, 1, 1, strides=[128, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %549 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %551 : Float(128, 512, 1, 1, strides=[512, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %552 : Float(128, strides=[1], requires_grad=0, device=cpu),\n",
            "      %554 : Float(128, 128, 3, 3, strides=[1152, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %555 : Float(128, strides=[1], requires_grad=0, device=cpu),\n",
            "      %557 : Float(512, 128, 1, 1, strides=[128, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %558 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %560 : Float(128, 512, 1, 1, strides=[512, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %561 : Float(128, strides=[1], requires_grad=0, device=cpu),\n",
            "      %563 : Float(128, 128, 3, 3, strides=[1152, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %564 : Float(128, strides=[1], requires_grad=0, device=cpu),\n",
            "      %566 : Float(512, 128, 1, 1, strides=[128, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %567 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %569 : Float(256, 512, 1, 1, strides=[512, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %570 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %572 : Float(256, 256, 3, 3, strides=[2304, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %573 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %575 : Float(1024, 256, 1, 1, strides=[256, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %576 : Float(1024, strides=[1], requires_grad=0, device=cpu),\n",
            "      %578 : Float(1024, 512, 1, 1, strides=[512, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %579 : Float(1024, strides=[1], requires_grad=0, device=cpu),\n",
            "      %581 : Float(256, 1024, 1, 1, strides=[1024, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %582 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %584 : Float(256, 256, 3, 3, strides=[2304, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %585 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %587 : Float(1024, 256, 1, 1, strides=[256, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %588 : Float(1024, strides=[1], requires_grad=0, device=cpu),\n",
            "      %590 : Float(256, 1024, 1, 1, strides=[1024, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %591 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %593 : Float(256, 256, 3, 3, strides=[2304, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %594 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %596 : Float(1024, 256, 1, 1, strides=[256, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %597 : Float(1024, strides=[1], requires_grad=0, device=cpu),\n",
            "      %599 : Float(256, 1024, 1, 1, strides=[1024, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %600 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %602 : Float(256, 256, 3, 3, strides=[2304, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %603 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %605 : Float(1024, 256, 1, 1, strides=[256, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %606 : Float(1024, strides=[1], requires_grad=0, device=cpu),\n",
            "      %608 : Float(256, 1024, 1, 1, strides=[1024, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %609 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %611 : Float(256, 256, 3, 3, strides=[2304, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %612 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %614 : Float(1024, 256, 1, 1, strides=[256, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %615 : Float(1024, strides=[1], requires_grad=0, device=cpu),\n",
            "      %617 : Float(256, 1024, 1, 1, strides=[1024, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %618 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %620 : Float(256, 256, 3, 3, strides=[2304, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %621 : Float(256, strides=[1], requires_grad=0, device=cpu),\n",
            "      %623 : Float(1024, 256, 1, 1, strides=[256, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %624 : Float(1024, strides=[1], requires_grad=0, device=cpu),\n",
            "      %626 : Float(512, 1024, 1, 1, strides=[1024, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %627 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %629 : Float(512, 512, 3, 3, strides=[4608, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %630 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %632 : Float(2048, 512, 1, 1, strides=[512, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %633 : Float(2048, strides=[1], requires_grad=0, device=cpu),\n",
            "      %635 : Float(2048, 1024, 1, 1, strides=[1024, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %636 : Float(2048, strides=[1], requires_grad=0, device=cpu),\n",
            "      %638 : Float(512, 2048, 1, 1, strides=[2048, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %639 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %641 : Float(512, 512, 3, 3, strides=[4608, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %642 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %644 : Float(2048, 512, 1, 1, strides=[512, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %645 : Float(2048, strides=[1], requires_grad=0, device=cpu),\n",
            "      %647 : Float(512, 2048, 1, 1, strides=[2048, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %648 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %650 : Float(512, 512, 3, 3, strides=[4608, 9, 3, 1], requires_grad=0, device=cpu),\n",
            "      %651 : Float(512, strides=[1], requires_grad=0, device=cpu),\n",
            "      %653 : Float(2048, 512, 1, 1, strides=[512, 1, 1, 1], requires_grad=0, device=cpu),\n",
            "      %654 : Float(2048, strides=[1], requires_grad=0, device=cpu)):\n",
            "  %496 : Float(1, 64, 112, 112, strides=[802816, 12544, 112, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[7, 7], pads=[3, 3, 3, 3], strides=[2, 2]](%input, %497, %498)\n",
            "  %323 : Float(1, 64, 112, 112, strides=[802816, 12544, 112, 1], requires_grad=1, device=cpu) = onnx::Relu(%496) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %324 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::MaxPool[kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[2, 2]](%323) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:659:0\n",
            "  %499 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%324, %500, %501)\n",
            "  %327 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Relu(%499) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %502 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%327, %503, %504)\n",
            "  %330 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Relu(%502) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %505 : Float(1, 256, 56, 56, strides=[802816, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%330, %506, %507)\n",
            "  %508 : Float(1, 256, 56, 56, strides=[802816, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%324, %509, %510)\n",
            "  %335 : Float(1, 256, 56, 56, strides=[802816, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Add(%505, %508) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %336 : Float(1, 256, 56, 56, strides=[802816, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Relu(%335)\n",
            "  %511 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%336, %512, %513)\n",
            "  %339 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Relu(%511) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %514 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%339, %515, %516)\n",
            "  %342 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Relu(%514) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %517 : Float(1, 256, 56, 56, strides=[802816, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%342, %518, %519)\n",
            "  %345 : Float(1, 256, 56, 56, strides=[802816, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Add(%517, %336) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %346 : Float(1, 256, 56, 56, strides=[802816, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Relu(%345)\n",
            "  %520 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%346, %521, %522)\n",
            "  %349 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Relu(%520) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %523 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%349, %524, %525)\n",
            "  %352 : Float(1, 64, 56, 56, strides=[200704, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Relu(%523) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %526 : Float(1, 256, 56, 56, strides=[802816, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%352, %527, %528)\n",
            "  %355 : Float(1, 256, 56, 56, strides=[802816, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Add(%526, %346) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %356 : Float(1, 256, 56, 56, strides=[802816, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Relu(%355) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %529 : Float(1, 128, 56, 56, strides=[401408, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%356, %530, %531)\n",
            "  %359 : Float(1, 128, 56, 56, strides=[401408, 3136, 56, 1], requires_grad=1, device=cpu) = onnx::Relu(%529) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %532 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[2, 2]](%359, %533, %534)\n",
            "  %362 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%532) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %535 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%362, %536, %537)\n",
            "  %538 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[2, 2]](%356, %539, %540)\n",
            "  %367 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Add(%535, %538) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %368 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%367)\n",
            "  %541 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%368, %542, %543)\n",
            "  %371 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%541) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %544 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%371, %545, %546)\n",
            "  %374 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%544) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %547 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%374, %548, %549)\n",
            "  %377 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Add(%547, %368) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %378 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%377)\n",
            "  %550 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%378, %551, %552)\n",
            "  %381 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%550) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %553 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%381, %554, %555)\n",
            "  %384 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%553) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %556 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%384, %557, %558)\n",
            "  %387 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Add(%556, %378) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %388 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%387)\n",
            "  %559 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%388, %560, %561)\n",
            "  %391 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%559) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %562 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%391, %563, %564)\n",
            "  %394 : Float(1, 128, 28, 28, strides=[100352, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%562) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %565 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%394, %566, %567)\n",
            "  %397 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Add(%565, %388) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %398 : Float(1, 512, 28, 28, strides=[401408, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%397) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %568 : Float(1, 256, 28, 28, strides=[200704, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%398, %569, %570)\n",
            "  %401 : Float(1, 256, 28, 28, strides=[200704, 784, 28, 1], requires_grad=1, device=cpu) = onnx::Relu(%568) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %571 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[2, 2]](%401, %572, %573)\n",
            "  %404 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%571) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %574 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%404, %575, %576)\n",
            "  %577 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[2, 2]](%398, %578, %579)\n",
            "  %409 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Add(%574, %577) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %410 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%409)\n",
            "  %580 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%410, %581, %582)\n",
            "  %413 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%580) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %583 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%413, %584, %585)\n",
            "  %416 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%583) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %586 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%416, %587, %588)\n",
            "  %419 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Add(%586, %410) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %420 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%419)\n",
            "  %589 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%420, %590, %591)\n",
            "  %423 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%589) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %592 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%423, %593, %594)\n",
            "  %426 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%592) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %595 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%426, %596, %597)\n",
            "  %429 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Add(%595, %420) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %430 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%429)\n",
            "  %598 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%430, %599, %600)\n",
            "  %433 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%598) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %601 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%433, %602, %603)\n",
            "  %436 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%601) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %604 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%436, %605, %606)\n",
            "  %439 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Add(%604, %430) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %440 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%439)\n",
            "  %607 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%440, %608, %609)\n",
            "  %443 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%607) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %610 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%443, %611, %612)\n",
            "  %446 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%610) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %613 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%446, %614, %615)\n",
            "  %449 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Add(%613, %440) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %450 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%449)\n",
            "  %616 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%450, %617, %618)\n",
            "  %453 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%616) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %619 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%453, %620, %621)\n",
            "  %456 : Float(1, 256, 14, 14, strides=[50176, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%619) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %622 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%456, %623, %624)\n",
            "  %459 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Add(%622, %450) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %460 : Float(1, 1024, 14, 14, strides=[200704, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%459) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %625 : Float(1, 512, 14, 14, strides=[100352, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%460, %626, %627)\n",
            "  %463 : Float(1, 512, 14, 14, strides=[100352, 196, 14, 1], requires_grad=1, device=cpu) = onnx::Relu(%625) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %628 : Float(1, 512, 7, 7, strides=[25088, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[2, 2]](%463, %629, %630)\n",
            "  %466 : Float(1, 512, 7, 7, strides=[25088, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Relu(%628) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %631 : Float(1, 2048, 7, 7, strides=[100352, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%466, %632, %633)\n",
            "  %634 : Float(1, 2048, 7, 7, strides=[100352, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[2, 2]](%460, %635, %636)\n",
            "  %471 : Float(1, 2048, 7, 7, strides=[100352, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Add(%631, %634) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %472 : Float(1, 2048, 7, 7, strides=[100352, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Relu(%471)\n",
            "  %637 : Float(1, 512, 7, 7, strides=[25088, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%472, %638, %639)\n",
            "  %475 : Float(1, 512, 7, 7, strides=[25088, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Relu(%637) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %640 : Float(1, 512, 7, 7, strides=[25088, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%475, %641, %642)\n",
            "  %478 : Float(1, 512, 7, 7, strides=[25088, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Relu(%640) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %643 : Float(1, 2048, 7, 7, strides=[100352, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%478, %644, %645)\n",
            "  %481 : Float(1, 2048, 7, 7, strides=[100352, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Add(%643, %472) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %482 : Float(1, 2048, 7, 7, strides=[100352, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Relu(%481)\n",
            "  %646 : Float(1, 512, 7, 7, strides=[25088, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%482, %647, %648)\n",
            "  %485 : Float(1, 512, 7, 7, strides=[25088, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Relu(%646) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %649 : Float(1, 512, 7, 7, strides=[25088, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%485, %650, %651)\n",
            "  %488 : Float(1, 512, 7, 7, strides=[25088, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Relu(%649) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %652 : Float(1, 2048, 7, 7, strides=[100352, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[1, 1], pads=[0, 0, 0, 0], strides=[1, 1]](%488, %653, %654)\n",
            "  %491 : Float(1, 2048, 7, 7, strides=[100352, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Add(%652, %482) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:138:0\n",
            "  %492 : Float(1, 2048, 7, 7, strides=[100352, 49, 7, 1], requires_grad=1, device=cpu) = onnx::Relu(%491) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1204:0\n",
            "  %493 : Float(1, 2048, 1, 1, strides=[2048, 1, 1, 1], requires_grad=1, device=cpu) = onnx::GlobalAveragePool(%492) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1037:0\n",
            "  %494 : Float(1, 2048, strides=[2048, 1], requires_grad=1, device=cpu) = onnx::Flatten[axis=1](%493) # /usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py:243:0\n",
            "  %output : Float(1, 1000, strides=[1000, 1], requires_grad=1, device=cpu) = onnx::Gemm[alpha=1., beta=1., transB=1](%494, %fc.weight, %fc.bias) # /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:1753:0\n",
            "  return (%output)\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hjBKrLWMGJHg"
      },
      "source": [
        "#ONNX to Tensorflow"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "v-4tdh_b2aUD",
        "outputId": "8706cc6a-f19c-4e9c-bd4b-a427263de786"
      },
      "source": [
        "!pip install onnx_tf\n",
        "from onnx_tf.backend import prepare\n",
        "import onnx\n",
        "\n",
        "TF_PATH = \"./converted.pb\" # where the representation of tensorflow model will be stored\n",
        "ONNX_PATH = \"./converted.onnx\" # path to my existing ONNX model\n",
        "onnx_model = onnx.load(ONNX_PATH)  # load onnx model\n",
        "\n",
        "# prepare function converts an ONNX model to an internel representation\n",
        "# of the computational graph called TensorflowRep and returns\n",
        "# the converted representation.\n",
        "tf_rep = prepare(onnx_model)  # creating TensorflowRep object\n",
        "\n",
        "# export_graph function obtains the graph proto corresponding to the ONNX\n",
        "# model associated with the backend representation and serializes\n",
        "# to a protobuf file.\n",
        "tf_rep.export_graph(TF_PATH)"
      ],
      "execution_count": 54,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: onnx_tf in /usr/local/lib/python3.7/dist-packages (1.8.0)\n",
            "Requirement already satisfied: onnx>=1.8.0 in /usr/local/lib/python3.7/dist-packages (from onnx_tf) (1.9.0)\n",
            "Requirement already satisfied: tensorflow-addons in /usr/local/lib/python3.7/dist-packages (from onnx_tf) (0.13.0)\n",
            "Requirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from onnx_tf) (3.13)\n",
            "Requirement already satisfied: protobuf in /usr/local/lib/python3.7/dist-packages (from onnx>=1.8.0->onnx_tf) (3.12.4)\n",
            "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from onnx>=1.8.0->onnx_tf) (1.15.0)\n",
            "Requirement already satisfied: typing-extensions>=3.6.2.1 in /usr/local/lib/python3.7/dist-packages (from onnx>=1.8.0->onnx_tf) (3.7.4.3)\n",
            "Requirement already satisfied: numpy>=1.16.6 in /usr/local/lib/python3.7/dist-packages (from onnx>=1.8.0->onnx_tf) (1.19.5)\n",
            "Requirement already satisfied: typeguard>=2.7 in /usr/local/lib/python3.7/dist-packages (from tensorflow-addons->onnx_tf) (2.7.1)\n",
            "Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from protobuf->onnx>=1.8.0->onnx_tf) (56.1.0)\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "WARNING:absl:Found untraced functions such as gen_tensor_dict while saving (showing 1 of 1). These functions will not be directly callable after loading.\n",
            "WARNING:absl:Found untraced functions such as gen_tensor_dict while saving (showing 1 of 1). These functions will not be directly callable after loading.\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "INFO:tensorflow:Assets written to: ./converted.pb/assets\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "INFO:tensorflow:Assets written to: ./converted.pb/assets\n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "TnF3AZ0pGNM7"
      },
      "source": [
        "#Tensorflow to TFLite"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZSYEAh7b7drX"
      },
      "source": [
        "# Convert the model\n",
        "converter = tf.lite.TFLiteConverter.from_saved_model(TF_PATH) # path to the SavedModel directory\n",
        "converter.target_spec.supported_ops = [\n",
        "  tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.\n",
        "  tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.\n",
        "]\n",
        "tflite_model = converter.convert()\n",
        "\n",
        "# Save the model.\n",
        "with open(model_name +\".tflite\", \"wb\") as f:\n",
        "  f.write(tflite_model)"
      ],
      "execution_count": 48,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hhGQR20AhLuX"
      },
      "source": [
        "#量子化も"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "id": "HIYMBv6oZNmz",
        "outputId": "955bbfc2-6e31-440d-8893-69e7bb330cbd"
      },
      "source": [
        "import pathlib\n",
        "\n",
        "# Convert the model\n",
        "converter = tf.lite.TFLiteConverter.from_saved_model(TF_PATH) # path to the SavedModel directory\n",
        "converter.target_spec.supported_ops = [\n",
        "  tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.\n",
        "  tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.\n",
        "]\n",
        "\n",
        "converter.optimizations = [tf.lite.Optimize.DEFAULT]\n",
        "tflite_quant_model = converter.convert()\n",
        "\n",
        "tflite_models_dir = pathlib.Path(\"/content/\")\n",
        "tflite_models_dir.mkdir(exist_ok=True, parents=True) \n",
        "\n",
        "tflite_model_quant_file = tflite_models_dir/\"resnet50_quant.tflite\"\n",
        "tflite_model_quant_file.write_bytes(tflite_model)\n",
        "\n",
        "\"\"\"\n",
        "# Save the model.\n",
        "with open(model_name +\".tflite\", \"wb\") as f:\n",
        "  f.write(tflite_model)\n",
        "\"\"\""
      ],
      "execution_count": 59,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'\\n# Save the model.\\nwith open(model_name +\".tflite\", \"wb\") as f:\\n  f.write(tflite_model)\\n'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 59
        }
      ]
    }
  ]
}